import pprint

import requests
from lxml import etree

# headers = {
#     "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
#     "accept-language": "zh-CN,zh;q=0.9",
#     "cache-control": "max-age=0",
#     "if-modified-since": "Thu, 09 Jan 2025 06:55:51 GMT",
#     "if-none-match": "\"3b86f856362db1:0\"",
#     "priority": "u=0, i",
#     "referer": "https://www.geyan123.cn/",
#     "sec-ch-ua": "\"Chromium\";v=\"134\", \"Not:A-Brand\";v=\"24\", \"Google Chrome\";v=\"134\"",
#     "sec-ch-ua-mobile": "?0",
#     "sec-ch-ua-platform": "\"Windows\"",
#     "sec-fetch-dest": "document",
#     "sec-fetch-mode": "navigate",
#     "sec-fetch-site": "same-origin",
#     "sec-fetch-user": "?1",
#     "upgrade-insecure-requests": "1",
#     "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36"
# }
# #
# cookies = {
#     "__51uvsct__3JxYmjWH7BbHTeTC": "1",
#     "__51vcke__3JxYmjWH7BbHTeTC": "b5db4fc6-6c91-58f8-809b-fe021d73d9c5",
#     "__51vuft__3JxYmjWH7BbHTeTC": "1743045114943",
#     "__vtins__3JxYmjWH7BbHTeTC": "%7B%22sid%22%3A%20%22b28cf97d-8a7c-5ada-a72a-23a70f82ad83%22%2C%20%22vd%22%3A%202%2C%20%22stt%22%3A%209565%2C%20%22dr%22%3A%209565%2C%20%22expires%22%3A%201743046924505%2C%20%22ct%22%3A%201743045124505%7D"
# }

cookies = {
    '__51vcke__3JxYmjWH7BbHTeTC': 'b5db4fc6-6c91-58f8-809b-fe021d73d9c5',
    '__51vuft__3JxYmjWH7BbHTeTC': '1743045114943',
    '__51uvsct__3JxYmjWH7BbHTeTC': '2',
    '__vtins__3JxYmjWH7BbHTeTC': '%7B%22sid%22%3A%20%22ba3aada0-f753-5aaa-aa13-4b0e8f172d9e%22%2C%20%22vd%22%3A%2011%2C%20%22stt%22%3A%20280998%2C%20%22dr%22%3A%2010%2C%20%22expires%22%3A%201743053342226%2C%20%22ct%22%3A%201743051542226%7D',
}

headers = {
    'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
    'accept-language': 'zh-CN,zh;q=0.9',
    'cache-control': 'max-age=0',
    'if-modified-since': 'Thu, 09 Jan 2025 06:55:51 GMT',
    'if-none-match': '"80b5fc846362db1:0"',
    'priority': 'u=0, i',
    'sec-ch-ua': '"Chromium";v="134", "Not:A-Brand";v="24", "Google Chrome";v="134"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"',
    'sec-fetch-dest': 'document',
    'sec-fetch-mode': 'navigate',
    'sec-fetch-site': 'none',
    'sec-fetch-user': '?1',
    'upgrade-insecure-requests': '1',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36'
}

def get_geyan(url, headers, cookies):
    r = requests.get(url, headers=headers, cookies=cookies)
    r.encoding = r.apparent_encoding
    html = etree.HTML(r.text)

    # 提取页面中的所有 <p> 标签的文本
    content = html.xpath('//div[@class="content"]/p/text()')
    if not content:
       context = html.xpath('/html/body/div[3]/div[1]/div[1]/div[4]/text()')
       # print(context)

    # 拼接所有 <p> 标签的文本
    full_content = ''.join(content)

    return full_content
# url = 'https://www.geyan123.cn/aiqinggeyan/1251.html'
#
# content = get_geyan(url, headers, cookies)


link_url = 'https://www.geyan123.cn/'
r = requests.get(link_url, headers=headers, cookies=cookies)
r.encoding = r.apparent_encoding
html = etree.HTML(r.text)
_link = html.xpath('//ul[@class="d1 ico3"]/li/a/@href')
link = list(dict.fromkeys(_link))

count = 1
# 遍历每个链接，获取内容并保存
for i in link:
    full_url = link_url.rstrip('/') + '/' + i.lstrip('/')
    print(f"正在获取链接{count}: {full_url}")  # 调试输出，查看每个链接
    cont = get_geyan(full_url, headers, cookies)
    print(f"第{count}页人生格言：" +cont)
    count += 1
    # if cont:  # 如果返回内容不是 None
    #     with open('人生格言.txt', 'a', encoding='utf-8') as f:  # 使用 'a' 模式追加写入
    #         f.write(cont + '\n')  # 确保写入内容
    # else:
    #     print(f"未能获取内容: {full_url}")
